package com.itheima.service.impl.store;

import com.github.pagehelper.PageHelper;
import com.itheima.domain.store.Orders;
import com.itheima.domain.store.Product;
import com.itheima.mapper.store.OrdersMapper;
import com.itheima.mapper.store.ProductMapper;
import com.itheima.service.store.ProductService;
import com.itheima.utils.IdUtil;
import com.itheima.utils.MapperUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class ProductServiceImpl implements ProductService {
    @Override
    //遍历
    public List<Product> list(int pageN, int pageS) {

        SqlSession session = MapperUtil.getSession();
        ProductMapper mapper = session.getMapper(ProductMapper.class);
        PageHelper.startPage(pageN, pageS);
        List<Product> productList= mapper.list();
        return productList;
    }

    @Override
    //新增
    public void save(Product product) {
        SqlSession session = MapperUtil.getSession();
        ProductMapper mapper = session.getMapper(ProductMapper.class);
        product.setId(IdUtil.getId());
        mapper.save(product);
        session.commit();
        MapperUtil.close(session);
    }

    //根据id删除
    @Override
    public void deleteById(String ids) {
        //通过pid删除oid
        SqlSession session = MapperUtil.getSession();
        ProductMapper mapper = session.getMapper(ProductMapper.class);
        OrdersServiceImpl ordersService=new OrdersServiceImpl();
        //通过pid查询oid
        List<String> oids= mapper.findOidByPid(ids);
        //调用ordersService执行删除
        for (String oid : oids) {
            ordersService.deleteById(oid);
        }
        //关闭session
        MapperUtil.close(session);
        //通过pid删除product
        SqlSession session1 = MapperUtil.getSession();
        ProductMapper mapper1 = session1.getMapper(ProductMapper.class);
        mapper1.deleteByIds(ids);
        session1.commit();
        MapperUtil.close(session1);
    }

    @Override
    //id查询
    public Product findById(String id) {
        SqlSession session = MapperUtil.getSession();
        ProductMapper mapper = session.getMapper(ProductMapper.class);
        Product product= mapper.findById(id);
        session.commit();
        MapperUtil.close(session);
        return product;
    }

    //编辑
    @Override
    public void edit(Product product) {
        SqlSession session = MapperUtil.getSession();
        ProductMapper mapper = session.getMapper(ProductMapper.class);
        mapper.edit(product);
        session.commit();
        MapperUtil.close(session);
    }

    @Override
    //开状态
    public void toon(String s) {
        SqlSession session = MapperUtil.getSession();
        ProductMapper mapper = session.getMapper(ProductMapper.class);
        mapper.toon(s);
        session.commit();
        MapperUtil.close(session);
    }

    @Override
    //关闭状态
    public void tooff(String s) {
        SqlSession session = MapperUtil.getSession();
        ProductMapper mapper = session.getMapper(ProductMapper.class);
        mapper.tooff(s);
        session.commit();
        MapperUtil.close(session);
    }

    @Override
    //找城市
    public List<String> findCity() {
        SqlSession session = MapperUtil.getSession();
        ProductMapper mapper = session.getMapper(ProductMapper.class);
        List<String> cityList=mapper.findAllCity();
        MapperUtil.close(session);
        return cityList;
    }

    @Override
    public List<Product> findByLike(String lms, int pageNum, int pageSize) {
        SqlSession session = MapperUtil.getSession();
        ProductMapper mapper = session.getMapper(ProductMapper.class);
        PageHelper.startPage(pageNum, pageSize);
        List<Product> productList= mapper.likeFindAll(lms);
        return productList;
    }

    @Override
    //异步
    public Product selectStatus(String id) {
        SqlSession session = MapperUtil.getSession();
        ProductMapper mapper = session.getMapper(ProductMapper.class);
        Product product= mapper.findById(id);
        return product;
    }


}
